<?php
/*
 * File blog.php for project Rijlesplanner.nl
 * Created on 07 aug 2008 by Niek Hoogma
 */
class blog extends modules {
	
	// Properties:
	protected $name = "home";
	protected $title = "Rijlesplanner - Goed geregeld!";
	protected $subtitle = "";
	
	// Methods:

	public function __construct() {
		parent :: __construct();
		$this->logger = parent :: getLogger();
		$this->user = parent :: getUser();
		$this->template = parent :: getTemplate();
	}

	function process(& $template = null) {
		$this->template = $template;
		$this->template->assign('moduleTemplate', 'modules/blog/blog.tpl');
		
		parent::loadModule("ads",$template);
		
		$blog = new DationBlog();
		
		$params = array();

		// Set max results to optimize query
		if (isset($_SESSION['config']['maxBlogResults']) && (int)$_SESSION['config']['maxBlogResults'] > 0) {
			$params['max-results'] = (int)$_SESSION['config']['maxBlogResults'];
		}
		
		// Set the maximum duration of posts to optimize queryies		
		if (isset($_SESSION['config']['maxPeriode']) && (int)$_SESSION['config']['maxPeriode'] > 0) {
			
			$ts = mktime() - (int)$_SESSION['config']['maxPeriode']*24*60*60;
			$params['published-min'] = (string)date("Y-m-d\T00:00:00",$ts);
		}
	
		$category = array();
		if (isset($_GET['category']) && $_GET['category'] != "") {
			$category[] = $_GET['category'];
			$this->template->assign('category',$_GET['category']);
		}
		
		if (isset($_GET['start-index']) && $_GET['start-index'] != "") {
			$params['start-index'] = $_GET['start-index'];
		}
		
		$blog_result = $blog->getPosts($params,$category);
		if ($blog_result['totalResults'] > $blog_result['itemsPerPage']) {
			
			// We need either previous or next scroll opportunities
			if (($blog_result['startIndex'] - 1 + $blog_result['itemsPerPage']) < $blog_result['totalResults']) {
				// next_button
				$this->template->assign('show_next',true);		
			} 
				
			if($blog_result['startIndex'] > 1) {
				// previous_button
				$this->template->assign('show_previous',true);		
			}
		}
		$this->template->assign('totalResults',$blog_result['totalResults']);
		$this->template->assign('startIndex',$blog_result['startIndex']);
		$this->template->assign('itemsPerPage',$blog_result['itemsPerPage']);
		
		$this->template->assign('entries', $blog_result['entries']);
		$this->template->assign('categories',$blog->getCategories());
	}
}